package com.jzy.util;

import java.io.Serializable;

public class PageUtil implements Serializable {

	//未处理的当前页 必须参数
	private Integer cPage;
	private int pageSize; //每页记录数
	private int count; //总记录数
	//非必须参数
	private int currentPage;//处理之后的当前页
	private int prevPage;//上一页
	private int nextPage;//下一页
	private int lastPage;//最后一页
	private int startIndex;//首页

	//是否为第一页
	private boolean isFirstPage = false;
	//是否为最后一页
	private boolean isLastPage = false;

	//是否有前一页
	private boolean hasPreviousPage = false;
	//是否有下一页
	private boolean hasNextPage = false;


	//导航页码数
	private int navigatePages = 8;
	//所有导航页号
	private int[] navigatepageNums;
	//导航条上的第一页
	private int navigateFirstPage;
	//导航条上的最后一页
	private int navigateLastPage;

	/**
	 * 判定页面边界
	 */
	private void judgePageBoudary() {
		isFirstPage = currentPage == 1;
		isLastPage = currentPage == lastPage;
		hasPreviousPage = currentPage > 1;
		hasNextPage = currentPage < lastPage;
	}

	public PageUtil(int cPage, int pageSize, int count){
		this.cPage = cPage;
		this.pageSize = pageSize;
		this.count = count;
		initCurrentPage();
		initPrevPage();
		initLastPage();
		initNextPage();
		initStartIndex();
		//计算导航页
		calcNavigatepageNums();
		//计算前后页，第一页，最后一页
		calcPage();
		//判断页面边界
		judgePageBoudary();
	}
	/**
	 * 计算导航页
	 */
	private void calcNavigatepageNums() {
		//当总页数小于或等于导航页码数时
		if (lastPage <= navigatePages) {
			navigatepageNums = new int[lastPage];
			for (int i = 0; i < lastPage; i++) {
				navigatepageNums[i] = i + 1;
			}
		} else { //当总页数大于导航页码数时
			navigatepageNums = new int[navigatePages];
			int startNum = currentPage - navigatePages / 2;
			int endNum = currentPage + navigatePages / 2;

			if (startNum < 1) {
				startNum = 1;
				//(最前navigatePages页
				for (int i = 0; i < navigatePages; i++) {
					navigatepageNums[i] = startNum++;
				}
			} else if (endNum > lastPage) {
				endNum = lastPage;
				//最后navigatePages页
				for (int i = navigatePages - 1; i >= 0; i--) {
					navigatepageNums[i] = endNum--;
				}
			} else {
				//所有中间页
				for (int i = 0; i < navigatePages; i++) {
					navigatepageNums[i] = startNum++;
				}
			}
		}
	}

	//初始化当前页 如果为空设为1
	private void initCurrentPage(){
		cPage = cPage == null ? 1 : cPage;
		currentPage = cPage;
	}

	/**
	 * 计算前后页，第一页，最后一页
	 */
	private void calcPage() {
		if (navigatepageNums != null && navigatepageNums.length > 0) {
			navigateFirstPage = navigatepageNums[0];
			navigateLastPage = navigatepageNums[navigatepageNums.length - 1];
			if (currentPage > 1) {
				prevPage = currentPage - 1;
			}
			if (currentPage < lastPage) {
				nextPage = currentPage + 1;
			}
		}
	}

	//初始化上一页
	private void initPrevPage(){
		if(currentPage == 1){
			prevPage = 1;
		}else{
			prevPage = currentPage - 1;
		}
	}
	
	//初始化最后一页
	private void initLastPage(){
		lastPage = count / pageSize;
		if(count % pageSize != 0){
			lastPage += 1;
		}
	}
	
	//初始化下一页
	private void initNextPage(){
		if(currentPage == lastPage){
			nextPage = currentPage;
		}else{
			nextPage = currentPage + 1;
		}
	}

	//初始化首页
	private void initStartIndex() {
		startIndex = (currentPage - 1) * pageSize;
	}

	public Integer getCpage() {
		return cPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getCount() {
		return count;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public int getPrevPage() {
		return prevPage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public int getLastPage() {
		return lastPage;
	}
	
	public int getStartIndex() {
		return startIndex;
	}

	public boolean isFirstPage() {
		return isFirstPage;
	}

	public boolean isLastPage() {
		return isLastPage;
	}

	public boolean isHasPreviousPage() {
		return hasPreviousPage;
	}

	public boolean isHasNextPage() {
		return hasNextPage;
	}

	public int getNavigatePages() {
		return navigatePages;
	}

	public int[] getNavigatepageNums() {
		return navigatepageNums;
	}

	public int getNavigateFirstPage() {
		return navigateFirstPage;
	}

	public int getNavigateLastPage() {
		return navigateLastPage;
	}
}
